package anx;

import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * Moves forward 150 pixels, then spins the radar. Turns 90 when it hits a wall.
 * @author Anthony Xu
 *
 */
public class Runner extends Robot {

  /**
   * moves forward 150 pixels, scans, then moves again.
   */
  public void run() {
    turnRight(correctHeading());
    //moves forward 150 pixels, then spins the radar 
    while (true) {
      ahead(150);
      turnGunRight(360);
    }    
  }
  
  /**
   * fixes the heading if its outside the range.
   * @return  angle to turn to get back to a correct heading
   */
  public double correctHeading() {
    double angle = 0;
    if ((getHeading() % 90) < 20) {
      angle = 20;
    }
    else if ((getHeading() % 90) > 70) {
      angle = -20;
    }
    return angle;
  }
  
  /**
   * shoots when a robot is found on the radar.
   * @param e   event when a robot is scanned
   */    
  public void onScannedRobot(ScannedRobotEvent e) {    
    fire(efficientFiring(e.getDistance()));
    scan();
  }
  
  /**
   * shoot with power proportional to distance.
   * formula from http://www.codepoet.org/~markw/weber/java/robocode/lesson4.html
   * @param distance  distance from enemy
   * @return power to shoot with
   */    
  public double efficientFiring(double distance) {
    double power;
    power = Math.min(400 / distance, 3);
    return power;
  }
  
  /**
   * Turns 90% if it hits a wall.
   * @param e   event when runner hits a wall
   */  
  public void onHitWall(HitWallEvent e) {   
    turnRight(90);   
  }
 
  
}